import requests
from bs4 import BeautifulSoup
import bs4
import py_mysql
import re
# 将提取后的结果追加到ulist列表中去
def fullTextToSchoolList(html):

    soup = BeautifulSoup(html, "html.parser")
    for tr in soup.find('tbody').children:
         if isinstance(tr,bs4.element.Tag):
            tds = tr('td')
            collage=tr("a")
            try:
                py_mysql.insertTable(tds[0].string.replace("\n","").strip(),
                                     collage[0].string.replace("\n","").strip(),
                                     tds[4].string.replace("\n","").strip())
            except:
                pass


def printSchoolList():
    print("{:^10}\t{:^10}\t{:^10}".format("排名","学校名称","分数"))


    list=py_mysql.selectTable()
    for i in list:
            print("{:^10}---{:^10}---{:^10}".format(i[0],i[1],i[2]))

#获取html
def getHTMLText(url):
    try:
        r = requests.get(url,timeout=3)
        r.raise_for_status()
        # 注意编码方式
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return "error!"
    return ""


def main():

    url = "http://www.shanghairanking.cn/rankings/bcur/2022"
    html = getHTMLText(url)
    fullTextToSchoolList(html)
    printSchoolList()
    #关闭资源
    py_mysql.close()

if __name__ == '__main__':
    main()

